import {defineStore} from "pinia";
import {reqFindAll, reqPhoneCode, reqUserLogin} from "@/api/hospital";
let useUserStore = defineStore('User',{
    state: () => {
        return {
            showLogin:false,
            code:'',
            userInfo:JSON.parse(localStorage.getItem('USERINFO') as string) || {},
            visitorInfo:[],
        }
    },
    actions:{
         //获取手机验证码
         async getCode(phone:string){
            let result = await (reqPhoneCode(phone));
            if (result.code === 200) {
                this.code = result.data;
                return 'ok'
            }else {
                Promise.reject(new Error(result.message))
            }
        },
        // 用户登录
        async phoneLogin(loginParam){
            let result =  await reqUserLogin(loginParam);
            if (result.code === 200) {
                this.userInfo = result.data;
                localStorage.setItem('USERINFO',JSON.stringify(result.data));
                return 'ok'
            }
            else {
                Promise.reject(new Error(result.message))
            }
        },
        // 用户点击退出登录
        logOut(){
             this.userInfo = {};
             localStorage.removeItem('USERINFO');
        },
        //获取用户就诊卡信息
        async getFindAll(){
            let result =  await reqFindAll();
            if (result.code === 200) {
                this.visitorInfo = result.data;
                return 'ok'
            }
            else {
                Promise.reject(new Error(result.message))
            }
        },



    },
    getters:{

    }
})

export default useUserStore;